import axios from 'axios'
import {
  getToken
} from '@/utils/localStorage.js'
const service = axios.create({
  baseURL: '/api',
  timeout: 5000
})

service.interceptors.request.use(
  (config) => {
    const token = getToken()
    if (token) {
      config.headers.Authorization = `Bearer ${token}`
    }
    return config
  }, err => {
    return Promise.reject(new Error(err))
  })
service.interceptors.response.use(res => {
  const {
    data
  } = res.data
  if (data.returnCode === '-1001') {
    return Promise.reject(new Error('失败'))
  } else {
    return data
  }
}, err => {
  return Promise.reject(err) // 向 catch 方法 返回一个错误对象
})

export default service
